package cn.iocoder.yudao.module.scrm.dal.mysql.customer;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.scrm.controller.admin.customer.vo.CustomerPageReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customer.CustomerPoolConfigDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

/**
 * SCRM客户 Mapper
 *
 * @author admin
 */
@Mapper
public interface CustomerMapper extends BaseMapperX<CustomerDO> {

    //吧客户的负责人id在传入的ids里面的客户全部查询出来
    default List<CustomerDO> selectCustomerByOwnerUserIds(List<Long> ids) {
        LambdaQueryWrapper<CustomerDO> query = new LambdaQueryWrapper<>();
        query.in(CustomerDO::getOwnerUserId, ids);

        return selectList(query);
    }

    default Long selectCustomerCountByOwnerUserIds(Long id) {
        LambdaQueryWrapper<CustomerDO> query = new LambdaQueryWrapper<>();
        query.in(CustomerDO::getOwnerUserId, id);

        return selectCount(query);
    }


    default Long selectCountByLockStatusAndOwnerUserId(Boolean lockStatus, Long ownerUserId) {
        return selectCount(new LambdaUpdateWrapper<CustomerDO>()
                .eq(CustomerDO::getLockStatus, lockStatus)
                .eq(CustomerDO::getOwnerUserId, ownerUserId));
    }

    default int updateOwnerUserIdByCustomerId(List<Long> customerIds, Long ownerUserId, String remark) {
        return update(new LambdaUpdateWrapper<CustomerDO>()
                .in(CustomerDO::getId, customerIds)
                .set(CustomerDO::getOwnerUserId, ownerUserId).set(CustomerDO::getRemark, remark));
    }

    default PageResult<CustomerDO> selectPage(CustomerPageReqVO reqVO) {
        LambdaQueryWrapperX<CustomerDO> query = new LambdaQueryWrapperX<>();
        if(Boolean.TRUE.equals(reqVO.getPool())){
            query.isNull(CustomerDO::getOwnerUserId);
        }else{
            query.isNotNull(CustomerDO::getOwnerUserId);
        }
        return selectPage(reqVO, query
                .inIfPresent(CustomerDO::getId, reqVO.getIds())
                .likeIfPresent(CustomerDO::getName, reqVO.getName())
                .likeIfPresent(CustomerDO::getEnterprise, reqVO.getEnterprise())
                .likeIfPresent(CustomerDO::getPhone, reqVO.getPhone())
                .eqIfPresent(CustomerDO::getClueSource, reqVO.getClueSource())
                .betweenIfPresent(CustomerDO::getCustomerSourceTime, reqVO.getCustomerSourceTime())
                .eqIfPresent(CustomerDO::getCustomerSource, reqVO.getCustomerSource())
                .eqIfPresent(CustomerDO::getMaterialSource, reqVO.getMaterialSource())
                .eqIfPresent(CustomerDO::getInfoSource, reqVO.getInfoSource())
                .eqIfPresent(CustomerDO::getRemark, reqVO.getRemark())
                .eqIfPresent(CustomerDO::getExtend, reqVO.getExtend())
                .eqIfPresent(CustomerDO::getFollowUpStatus, reqVO.getFollowUpStatus())
                .betweenIfPresent(CustomerDO::getContactLastTime, reqVO.getContactLastTime())
                .eqIfPresent(CustomerDO::getContactLastContent, reqVO.getContactLastContent())
                .betweenIfPresent(CustomerDO::getContactNextTime, reqVO.getContactNextTime())
                .inIfPresent(CustomerDO::getOwnerUserId, reqVO.getOwnerUserId())
                .betweenIfPresent(CustomerDO::getOwnerTime, reqVO.getOwnerTime())
                .eqIfPresent(CustomerDO::getDealStatus, reqVO.getDealStatus())
                .eqIfPresent(CustomerDO::getLockStatus, reqVO.getLockStatus())
                .eqIfPresent(CustomerDO::getLevel, reqVO.getLevel())
                .eqIfPresent(CustomerDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(CustomerDO::getCreateTime, reqVO.getCreateTime())
                 //字典
                .eqIfPresent(CustomerDO::getCustomerStage, reqVO.getCustomerStage())
                .eqIfPresent(CustomerDO::getYearTurnover, reqVO.getYearTurnover())
                .eqIfPresent(CustomerDO::getSonCompany, reqVO.getSonCompany())
                .eqIfPresent(CustomerDO::getReceivableMethod, reqVO.getReceivableMethod())

                .likeIfPresent(CustomerDO::getPosition, reqVO.getPosition())
                .likeIfPresent(CustomerDO::getIdCard, reqVO.getIdCard())
                .likeIfPresent(CustomerDO::getInviter, reqVO.getInviter())
                //.likeIfPresent(CustomerDO::getReceivablePrice, String.valueOf(reqVO.getReceivablePrice()))
                .betweenIfPresent(CustomerDO::getReceivableTime, reqVO.getReceivableTime())
                .eqIfPresent(CustomerDO::getSummitStatus, reqVO.getSummitStatus())
                .likeIfPresent(CustomerDO::getSummitGroup, reqVO.getSummitGroup())
                .likeIfPresent(CustomerDO::getNumberPeople, reqVO.getNumberPeople())
                .likeIfPresent(CustomerDO::getQuantityPain, reqVO.getQuantityPain())


                .orderByDesc(CustomerDO::getId));
    }

    default int updateOwnerUserIdById(Long id, Long ownerUserId) {
        return update(new LambdaUpdateWrapper<CustomerDO>()
                .eq(CustomerDO::getId, id)
                .set(CustomerDO::getOwnerUserId, ownerUserId));
    }

    default List<CustomerDO> selectListByAutoPool(CustomerPoolConfigDO poolConfig) {
        LambdaQueryWrapper<CustomerDO> query = new LambdaQueryWrapper<>();
        query.gt(CustomerDO::getOwnerUserId, 0);
        // 未锁定 + 未成交
        query.eq(CustomerDO::getLockStatus, false).eq(CustomerDO::getDealStatus, false);
        // 已经超时
        LocalDateTime dealExpireTime = LocalDateTime.now().minusDays(poolConfig.getDealExpireDays());
        LocalDateTime contactExpireTime = LocalDateTime.now().minusDays(poolConfig.getContactExpireDays());
        query.and(q -> {
            // 情况一：成交超时
            q.lt(CustomerDO::getOwnerTime, dealExpireTime)
                    // 情况二：跟进超时
                    .or(w -> w.lt(CustomerDO::getOwnerTime, contactExpireTime)
                            .and(p -> p.lt(CustomerDO::getContactLastTime, contactExpireTime)
                                    .or().isNull(CustomerDO::getContactLastTime)));
        });
        return selectList(query);
    }

}